package com.base.core.dao;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.base.core.domain.BaseDomain;



public class BaseDao extends SqlSessionDaoSupport implements IBaseDao {
	private static final String EXPAND = "expand";
	private static final String UPDATE = "update";
	private static final String INSERT = "insert";
	private static final String QUERY = "query";
	
	
	@Override
	public BaseDomain expandDomain(BaseDomain domain) {		
		return (BaseDomain) getSqlSession().selectOne(domain.getSqlMapSpace()+"."+EXPAND, domain);
	}
 
	@Override
	public BaseDomain expandDomain(BaseDomain domain, String sqlMap) {
		return (BaseDomain) getSqlSession().selectOne(sqlMap, domain);
	}

	@Override
	public int updateDomain(String sqlMap) {
		return getSqlSession().update(sqlMap);
	}
	
	

	@Override
	public int updateDomain(BaseDomain domain) {
		return getSqlSession().update(domain.getSqlMapSpace()+"."+UPDATE, domain);
	}

	@Override
	public int updateDomain(BaseDomain domain, String sqlMap) {
		return getSqlSession().update(sqlMap, domain);
	}

	@Override
	public int insertDomain(String sqlMap) {
		return getSqlSession().insert(sqlMap);
	}

	@Override
	public int insertDomain(BaseDomain domain) {
		return getSqlSession().insert(domain.getSqlMapSpace()+"."+INSERT, domain);
	}

	@Override
	public int insertDomain(BaseDomain domain, String sqlMap) {
		return getSqlSession().insert(sqlMap, domain);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BaseDomain> queryDomain(String sqlMap) {
		return getSqlSession().selectList(sqlMap);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<BaseDomain> queryDomain(BaseDomain domain) {
		try{
		List list = getSqlSession().selectList(domain.getSqlMapSpace()+"."+QUERY, domain);
		return list;
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<BaseDomain> queryDomain(BaseDomain domain, String sqlMap) {
		return getSqlSession().selectList(sqlMap, domain);
	}

	@SuppressWarnings({ "rawtypes" })
	@Override
	public List<Map> queryMap(String sqlMap) {

		return null;
	}

	@SuppressWarnings({ "rawtypes" })
	@Override
	public List queryMap(BaseDomain domain) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings({ "rawtypes" })
	@Override
	public List queryMap(BaseDomain domain, String sqlMap) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings({ "rawtypes" })
	@Override
	public List queryMap(Map map) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings({ "rawtypes" })
	@Override
	public List queryMap(Map map, String sqlMap) {
		// TODO Auto-generated method stub
		return null;
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List queryDomainWithPage(String sqlMap, int page, int limit) {
		return queryDomainWithPage(null, sqlMap, page, limit);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List queryDomainWithPage(BaseDomain domain, int page, int limit) {
		// TODO Auto-generated method stub
		return queryDomainWithPage(domain, domain.getSqlMapSpace()+"."+QUERY, page, limit);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List queryDomainWithPage(BaseDomain domain, String sqlMap, int page,
			int limit) {
		RowBounds round = new RowBounds(page * limit, limit);
		return getSqlSession().selectList(sqlMap, domain, round);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List queryMapWithPage(String sqlMap, int page, int limit) {
		return queryMapWithPage(null, sqlMap, page, limit);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List queryMapWithPage(Map map, String sqlMap, int page, int limit) {
		RowBounds round = new RowBounds(page * limit, limit);
		return getSqlSession().selectList(sqlMap, map, round);

	}
}
